<template>
  <div class="providePage">
    <h3>provide / inject (长距离的props) 的示例</h3>
    <p>响应式数据的实现：内层组件inject，接收外层组件provide提供的返回值的函数，同时使用computed接收函数的返回值</p>
    <div class="topComponent">
      <div class="title">顶部组件</div>
      <div class="content">
        <button @click="changeStatic">改变数据</button>
        <div>顶层组件中的数据：{{ provideData }}</div>
      </div>
      <midleComponent></midleComponent>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import midleComponent from "./components/midleComponent.vue";
export default defineComponent({
  components: {
    midleComponent,
  },
  props: {
    user: {
      type: String,
      required: true,
    },
  },
  emits: [],
  data() {
    return {
      provideData: "广州",
    };
  },
  provide() {
    return {
      dynamicProvideData: () => this.provideData,
      staticProvideData: this.provideData,
    };
  },
  methods: {
    changeStatic() {
      this.provideData = "珠海";
    },
  },
});
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
.providePage {
  width: 600px;
  margin: 0 auto;
  h3 {
    margin: 40px 0 0;
  }
  p {
    color: $myRedColor;
  }
  .topComponent {
    background: $myRedColor;
    height: 600px;
    .title {
      color: #000;
      text-align: left;
    }
    .content {
      color: #fff;
    }
  }
}
</style>
